<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('login/login_model');
        $this->load->helper('cookie');
	}
	
	public function index()
	{
        if($this->session->userdata('username'))
        {
            redirect(site_url('home'));
            die();
        }
	   $data = array();
       $error = array();
       $error['username'] = '';
       $error['password'] = '';
       $info = array();
       $info['username'] = '';
       $info['password'] = '';
       
       // thong tin remember username & password;
       
       $ww_username = trim($this->input->cookie('ww_username'));
       $ww_password = trim($this->input->cookie('ww_password'));
       if($ww_username == '' && $ww_password == '')
       {
           if($_SERVER['REQUEST_METHOD'] == 'POST')
           {
                $info['username'] = $this->input->post('username');
                $info['password'] = $this->input->post('password');
                $remember = $this->input->post('remember');
                
                $username = $this->mycommon->getSaveSqlStr($this->input->post('username'));
                $password = $this->mycommon->getSaveSqlStr($this->input->post('password'));
                
                $error['username'] = $username == '' ? 'Bạn hãy điền tên đăng nhập' : '';
                $error['password'] = $password == '' ? 'Bạn hãy điền mật khẩu' : '';
                
                if($error['username'] == '' && $error['password'] == '')
                {
                    $user_info = $this->login_model->ww_check_login($username);
                    if($user_info)
                    {
                        if(md5(sha1($password)) == $user_info['password'])
                        {
                            if(strlen($user_info['active']) != 32)
                            {
                                $this->session->set_userdata('username', $user_info['username']);
                                $this->session->set_userdata('access', $user_info['access']);
                                $this->session->set_userdata('allianceid', $user_info['aid']);
                                $this->session->set_userdata('uid', $user_info['id']);
                                $this->session->set_userdata('oasis_orderid', $user_info['oasis_orderid']);
                                // check remember
    
                                if($remember != false && $ww_username =='' && $ww_password == '')
                                {
                                    $this->input->set_cookie('username',$user_info['username'],60*60*24*3,'','/','ww_',false);
                                    $this->input->set_cookie('password',$user_info['password'],60*60*24*3,'','/','ww_',false);
                                }
                                redirect(site_url("home"));
                                die();
                                //  redirect to other page
                            }
                            else
                            {
                                $error['username'] = 'Tài khoản chưa kích hoạt.';
                            }
                        }
                        else
                        {
                            $error['password'] = 'Mật khẩu không chính xác';
                        }
                    }
                    else
                    {
                        $error['username'] = 'Tài khoản không tồn tại';
                    }
                }
           }
        }
        else
        {
            $username = $this->mycommon->getSaveSqlStr($ww_username);
            $password = $this->mycommon->getSaveSqlStr($ww_password);
            
            $user_info = $this->login_model->ww_check_login($username);
            
            if($user_info)
            {
                if($password == $user_info['password'])
                {
                    $this->session->set_userdata('username', $user_info['username']);
                    $this->session->set_userdata('access', $user_info['access']);
                    $this->session->set_userdata('allianceid', $user_info['aid']);
                    $this->session->set_userdata('uid', $user_info['id']);
                    $this->session->set_userdata('oasis_orderid', $user_info['oasis_orderid']);
                    redirect(site_url("home"));
                    die();
                }
                else
                {
                    $error['password'] = 'Mật khẩu không chính xác';
                }
            }
        }
        $data['error'] = $error;
        $data['info'] = $info;
        // header
        
        // content
        $this->load->view('login/login_view', $data);
        
        // foter
	}
}